package com.et114.modules.cust.action;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.et114.core.action.BaseDispatchAction;
import com.et114.core.common.CONST;
import com.et114.modules.cust.form.Mng_CustForm;
import com.et114.modules.cust.services.IMng_CustService;
import com.et114.modules.cust.services.Mng_CustServiceImp;
import com.et114.modules.cust.vo.CustBaseVo;
import com.et114.modules.cust.vo.LeadVo;
import com.et114.modules.org.utility.SessionUserInfo;
import com.et114.modules.system.dictionary.DictionaryCache;
import com.et114.modules.system.dictionary.vo.BizOption;

/**
 * 客户信息修改
 * @author zhiweip
 * @version 1.0
 * @created 09-十一月-2010 23:03:48
 */
public class Mng_CustModifyAction  extends BaseDispatchAction {

	private IMng_CustService mng_CustService = ( Mng_CustServiceImp )getBean ( "mng_CustService" ) ;
	@ Override
	protected ActionForward unspecified ( ActionMapping mapping ,
			ActionForm form , HttpServletRequest request ,
			HttpServletResponse response ) throws Exception {
		// TODO Auto-generated method stub
		return super.unspecified ( mapping , form , request , response );
	}

	//Mng_CustForm
	public ActionForward doModify ( ActionMapping mapping ,
			ActionForm form , HttpServletRequest request ,
			HttpServletResponse response ) throws Exception {
		
		request.setAttribute ( "OPTIONS" , mng_CustService.getItemTypes ( ) ) ;
		
		Mng_CustForm cform=(Mng_CustForm)form;
		 SessionUserInfo sessionuserinfo = getCurrentUser(request);
		 CustBaseVo cust= cform.getCust ( );
		 cust.setCrtUser ( sessionuserinfo.getUserId ( ) );
		 cust.setUpdtUser ( sessionuserinfo.getUserId ( ) );
		Integer custid=mng_CustService.newCust ( cust ,request);
		request.setAttribute ( "custid",custid);
		
		String next=request.getParameter ( "nextstp" );
		if(next!=null){
			if(next.equalsIgnoreCase ( "update" )){
				request.setAttribute ( "back" , "Mng_CustQueryAction.go?method=custQuery" );
				return mapping.findForward ( "success" ) ; 
			}else if(next.equalsIgnoreCase ( "toorder" )){
				request.setAttribute ( "back" , "Mng_OrderQueryAction.go?method=chooseTypes" );
				response.sendRedirect ( "Mng_OrderQueryAction.go?method=chooseTypes&opflag=fromCust&custid="+custid+"&comeFrom=Mng_CustQueryAction.go");
				return mapping.findForward ( "success" ) ; 
			}
		}
		
		//if(cust.getCustid ( )==null){
			LeadVo lvo=new LeadVo();
			lvo.setCustid ( custid );
			request.setAttribute ( "LeaderInfo" ,mng_CustService.loadLeader(lvo));
			request.setAttribute ( "LeaderINTENT" ,mng_CustService.queryLeaderIntentList ( lvo ));
			request.setAttribute ( "back" , "Mng_CustQueryAction.go?method=custQuery" );
			DictionaryCache bean = DictionaryCache.getInstance ( ); 
			List < BizOption > options = bean.listBizOptions (
					"BUYTYPE", false , CONST.COUNTRY_ZH );
			
			request.setAttribute ( "BTYPE" , options);
		//}
		return mapping.findForward ( "intention" ) ; 
	}
	
	public ActionForward doTrack ( ActionMapping mapping ,
			ActionForm form , HttpServletRequest request ,
			HttpServletResponse response ) throws Exception {
		request.setAttribute ( "OPTIONS" , mng_CustService.getItemTypes ( ) ) ;
		Integer custid=new Integer(request.getParameter ( "custid" ));
		String opflag=request.getParameter ( "opflag" );
		
		request.setAttribute ( "custid",custid); 
		request.setAttribute ( "opflag",opflag); 
		LeadVo lvo=new LeadVo();
		lvo.setCustid ( custid );
		lvo=mng_CustService.loadLeader(lvo);
		request.setAttribute ( "LeaderInfo" ,lvo);
		if(lvo!=null){
			LeadVo ivo=new LeadVo();
			ivo.setLeaderid ( lvo.getLeaderid ( ) );
			request.setAttribute ( "LeaderINTENT" ,mng_CustService.queryLeaderIntentList ( ivo ));
			request.setAttribute ( "LeaderINTENTLOG" ,mng_CustService.query_PUB_LEADER_LOG (ivo ));
		}
		DictionaryCache bean = DictionaryCache.getInstance ( ); 
		List < BizOption > options = bean.listBizOptions (
				"BUYTYPE", false , CONST.COUNTRY_ZH );
		
		request.setAttribute ( "BTYPE" , options);
		return mapping.findForward ( "intention" ) ; 
	}
	
	public ActionForward saveIntent ( ActionMapping mapping ,
			ActionForm form , HttpServletRequest request ,
			HttpServletResponse response ) throws Exception {
		SessionUserInfo sessionuserinfo = getCurrentUser(request);
		Mng_CustForm cform=(Mng_CustForm)form; 
		LeadVo cust=cform.getCust ( ); 		
		cust.setCrtUser ( sessionuserinfo.getUserId ( ) );
		cust.setUpdtUser ( sessionuserinfo.getUserId ( ) ); 

		mng_CustService.update_Lead_Intent ( cust , request );
		
		String opflag=cust.getOpflag ( );
		if(opflag!=null&&opflag.equalsIgnoreCase ( "lead" )){
			request.setAttribute ( "back" , "Mng_LeadQueryAction.go?method=leadQuery" );
		}else
			request.setAttribute ( "back" , "Mng_CustQueryAction.go" );
		return mapping.findForward ( "success" ) ; 
	}
	
	 

}